<template>
  <el-dialog :visible="showDialog" title="新增员工" @close="closeFn">
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @focus="isShow = true"
        />
        <el-tree
          v-show="isShow"
          :data="departmentList"
          :props="{ label: 'name' }"
          default-expand-all
          @node-click="selectDepartment"
        />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-row type="flex" justify="center">
        <el-button
          size="small"
          type="primary"
          @click="confirmFn"
        >确定</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </span>
  </el-dialog>
</template>

<script>
import employees from '@/api/constant/employees.js'
import { getDepartments } from '@/api/departments'
import { listToTree } from '@/utils/index'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isShow: false,
      hireType: employees.hireType,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空！', trigger: 'blur' },
          { min: 1, max: 10, message: '用户名长度1到10为', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空！', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空！', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空！', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门名称不能为空！', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空！', trigger: 'blur' }
        ]
      },
      departmentList: []
    }
  },
  async created() {
    // 获取部门列表数据
    const res = await getDepartments()
    this.departmentList = listToTree(res.depts, '')
  },
  methods: {
    // 确定 新增
    async confirmFn() {
      // 表单验证
      await this.$refs.form.validate()
      // 调用接口 实现新增
      await addEmployee(this.formData)
      // 关闭弹窗
      this.closeFn()
      // 刷新列表
      this.$emit('updateEvent')
    },
    closeFn() {
      // 清空数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 清空校验
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    // 选中部门
    selectDepartment(data) {
      // console.log(109, data)
      this.formData.departmentName = data.name
      this.isShow = false
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form-item {
  position: relative;
  .el-tree {
    height: 150px;
    width: 50%;
    position: absolute;
    z-index: 1;
    overflow-y: auto;
  }
}
</style>
<style>
::-webkit-scrollbar-track-piece {
  border-radius: 0
}
::-webkit-scrollbar {
  width: 5px;
  height: 10px
}
::-webkit-scrollbar-thumb {
  height: 50px;
  background-color: #b8b8b8;
  border-radius: 6px;
  outline-offset: -2px;
  filter: alpha(opacity = 50);
  -moz-opacity: 0.5;
  -khtml-opacity: 0.5;
  opacity: 0.5
}
::-webkit-scrollbar-thumb:hover {
  height: 50px;
  background-color: #878987;
  border-radius: 6px
}
</style>
